<template>
  <div class="sachertorteBand" :style="{ '--color': `${background_color}` }">
    <c-title :hide="false" text="品牌馆"></c-title>
    <van-search @blur="getBand" show-action v-model="keyword" placeholder="品牌推荐" shape="round">
      <template #action>
        <div @click="getBand" class="searchBtn">搜索</div>
      </template>
    </van-search>
    <div class="handpick">
      <div class="handpick-list" v-for="(item, index) in recommandList" :key="index" @click="bandDetail(item)">
        <div class="top">
          <img :src="item.logo" alt="" class="usetPhoto" />
          <div class="top_right">
            <div class="bandName">{{ item.name }}</div>
            <div class="localDetail" v-if="item.has_one_store">
              <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2Fsachertorte%2Flocal%20(1)%402x.png" alt="" />
              <span class="detail">{{ item.has_one_store.address }}</span>
              <div class="distance">{{ setDistance(item.has_one_store.distance) }}</div>
            </div>
          </div>
        </div>
        <div class="bandShop">
          <div class="lis" v-for="kitem in item.has_many_goods" :key="kitem.id" @click.stop="goodsNav(kitem.id)">
            <img :src="kitem.thumb" alt="" />
            <div class="shopName">{{ kitem.title }}</div>
            <div class="price">￥{{ kitem.price }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      page: 1,
      totalPage: 0,
      isLoadMore: true,
      city: "",
      point:{},
      recommandList: [],
      keyword:'',
      background_color:'#ffc11e'
    };
  },
  activated() {
    this.keyword = '';
    this.page = 1;
    if (this.$route.query.city) {
      this.city = this.$route.query.city.trim();
    }
    this.fun
      .getLocation()
      .then(res => {
        this.point = res.point;
        if (this.city === "") {
          this.city = res.city;
        }
        this.getBand();
      })
      .catch(err => {
        console.log(err);
        this.getBand();
      });
  },
  methods: {
    goodsNav(id) {
      this.$router.push(this.fun.getUrl("sachertorteGoods", {}, { id: id, city: this.city }));
    },
    bandDetail(item){
      console.log(item.brand_id)
      this.$router.push(this.fun.getUrl("sachertorteBandShop", {brand_id:item.id}, {city:this.city}));
    },
    setDistance(distance){
      if(distance >= 1000){
        return Number(distance/1000).toFixed(2) + 'Km'
      }else{
        return distance+'m'
      }
    },
    init(){
      this.page = 1;
      this.total_page = 0;
      this.isLoadMore = true;
      this.recommandList = [];
    },
    getBand() {
      this.init();
      let json = {
        city: this.city,
        page: this.page,
        lng: this.point.lng ? this.point.lng : "",
        lat: this.point.lat ? this.point.lat : "",
        brand_name:this.keyword
      };
      $http.get("plugin.yz-supply-cake.frontend.goods.goods.getBrandAndGoodsAndStore", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.total_page = res.last_page;
            if (!this.total_page) {
              this.page = 0;
            }
            this.recommandList = res.data;
            let info_basic = JSON.parse(localStorage.getItem("yz_basic_info"));
            this.background_color = info_basic.theme_color;
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        Toast.loading({
          message: '加载中...',
          duration: 0,
        });
        this.page = this.page + 1;
        let json ={
          city:this.city,
          page:this.page,
          lng:this.point.lng?this.point.lng:'',
          lat:this.point.lat?this.point.lat:'',
          brand_name:this.keyword
        }
        $http.get("plugin.yz-supply-cake.frontend.goods.goods.getBrandAndGoodsAndStore", json).then(
          res => {
            Toast.clear();
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.data;
              this.recommandList = [...this.recommandList, ...myData]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.searchBtn {
  font-size: 0.75rem;
  line-height: 1.03rem;
  color: #fff;
  padding: 0.25rem 1rem;
  background: var(--color);
  border-radius: 1.09rem;
}

.handpick {
  margin: 0.75rem 0.75rem;
}

.handpick-list {
  border-radius: 0.5rem;
  margin-top: 0.5rem;
  padding: 0.75rem;
  background: #fff;
  overflow: hidden;
  // width: 348px;
  // overflow-x: scroll;
  .bandShop {
    margin-top: 0.75rem;
    display: flex;
    justify-content: flex-start;

    .lis {
      display: flex;
      flex-direction: column;
      margin-right: 0.47rem;
      width: 4.75rem;
      overflow: hidden;

      img {
        width: 4.75rem;
        height: 4.75rem;
        border-radius: 0.25rem;
        box-shadow: 0 0 0 rgba(0, 0, 0, 0.1);
      }

      .shopName {
        font-size: 0.75rem;
        color: #333;
        line-height: 1.03rem;
        margin: 0.22rem 0 0.25rem 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .price {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: 0.75rem;
        color: #e32323;
        line-height: 1.03rem;
      }
    }
  }

  .top {
    display: flex;

    .usetPhoto {
      flex-shrink: 0;
      width: 2.5rem;
      height: 2.5rem;
      border-radius: 1.25rem;
      margin-right: 0.25rem;
    }

    .top_right {
      flex: 1;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .bandName {
        font-size: 0.88rem;
        color: #333;
        font-weight: bold;
        text-align: left;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .localDetail {
        display: flex;
        align-items: center;
        color: #999;
        font-size: 0.75rem;

        img {
          width: 0.75rem;
          height: 0.75rem;
          flex-shrink: 0;
          margin-right: 0.25rem;
        }

        .detail {
          color: #999;
          flex: 1;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          margin-right: 0.84rem;
          text-align: left;
        }

        .distance {
          flex-shrink: 0;
        }
      }
    }
  }
}
</style>
